/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mysweethome.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author johan
 */
public  abstract  class AbstractBaseDAO<T> {

    @PersistenceContext(unitName = "MySweetHome_ejbPU")
    protected EntityManager em;

    public AbstractBaseDAO(EntityManager em) {
        this.em = em;
    }


    /**
     * Create new object and update into database
     * @param entity - new object
     * @return - true if success
     */
    public boolean  create(T entity){
        boolean rs = false;
        try {
            em.persist(entity);
            rs = true;
        } catch (Exception e) {
            e.printStackTrace();           
        }finally{
            em.close();
            return rs;
        }
    }


    /**
     * update object into database
     * @param entity - updated object
     * @return : true if success
     */
    public boolean update(T entity){
        boolean rs = false;
        try {
            em.merge(entity);
            rs = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            em.close();
            return rs;
        }
    }

    public boolean delete(T entity){
         boolean rs = false;
        try {
            em.remove(entity);
            rs = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            em.close();
            return rs;
        }
    }



    


}
